package org.springsocial.services.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springsocial.dao.TweetDao;
import org.springsocial.domain.ExtendedTweet;
import org.springsocial.services.TweetService;

@Service
public class TweetServiceImpl implements TweetService {
	@Autowired 
	TweetDao tweetDao;
	
	@Override
	@Transactional(readOnly=false)
	public ExtendedTweet saveTweet(ExtendedTweet tweet) {
		return tweetDao.saveTweet(tweet);
	}

	@Override
	public ExtendedTweet getTweetById(Long id) {
		return tweetDao.getTweetById(id);
	}

	@Override
	public List<ExtendedTweet> getTweetsFromToDate(Date from, Date to) {
		return tweetDao.getTweetsFromToDate(from, to);
	}

	@Override
	public List<ExtendedTweet> getTweetsFromLastMonths(int numberOfMonths) {
		Calendar from = Calendar.getInstance();
		Calendar to = Calendar.getInstance();
		from.add(Calendar.MONTH, numberOfMonths);
		return  tweetDao.getTweetsFromToDate(from.getTime(), to.getTime());
	}
	@Override
	public List<ExtendedTweet> getTweetsFromLastDays(int numberOfDays) {
		Calendar from = Calendar.getInstance();
		Calendar to = Calendar.getInstance();
		from.add(Calendar.DAY_OF_MONTH, -numberOfDays);
		return  tweetDao.getTweetsFromToDate(from.getTime(), to.getTime());
	}
	

}
